import objectUtils from "@/utils/object/index.js";

export default objectUtils.freeze({
	formatTimeStr(type){
		switch(type){
			case 1:
			  return '上午'
			case 2:
			  return '下午'
			case 3:
			  return '晚上'
			case 4:
			  return '凌晨'
		}
	},
	create: function(format){
		let fmt = format || "yyyy-MM-dd hh:mm:ss"
		let time = new Date();
		var o = {
		    "M+": time.getMonth() + 1, //月份
		    "d+": time.getDate(), //日
		    "h+": time.getHours(), //小时
		    "m+": time.getMinutes(), //分
		    "s+": time.getSeconds(), //秒
		    "q+": Math.floor((time.getMonth() + 3) / 3), //季度
		    "S": time.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt))
		    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
		    if (new RegExp("(" + k + ")").test(fmt))
		        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	},
	createTomorrow: function(format){
		let fmt = format || "yyyy-MM-dd hh:mm:ss"
		let time = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
		var o = {
		    "M+": time.getMonth() + 1, //月份
		    "d+": time.getDate(), //日
		    "h+": time.getHours(), //小时
		    "m+": time.getMinutes(), //分
		    "s+": time.getSeconds(), //秒
		    "q+": Math.floor((time.getMonth() + 3) / 3), //季度
		    "S": time.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt))
		    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
		    if (new RegExp("(" + k + ")").test(fmt))
		        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	},
	format: function(dateNumber, format){
		if(dateNumber){
			let fmt = format || "yyyy-MM-dd hh:mm:ss"
			let time = new Date(dateNumber);
			var o = {
			    "M+": time.getMonth() + 1, //月份
			    "d+": time.getDate(), //日
			    "h+": time.getHours(), //小时
			    "m+": time.getMinutes(), //分
			    "s+": time.getSeconds(), //秒
			    "q+": Math.floor((time.getMonth() + 3) / 3), //季度
			    "S": time.getMilliseconds() //毫秒
			};
			if (/(y+)/.test(fmt))
			    fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
			for (var k in o)
			    if (new RegExp("(" + k + ")").test(fmt))
			        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			return fmt;
		}else{
			return "";
		}
	},
	formatTime: function(date){
		let re = new RegExp("-", "gi");
		let time = new Date(date.replace(re, "/"));
		return time.getTime();
	},
	distanceCurrentDays: function(date){
		let re = new RegExp("-", "gi");
		let time = new Date(date.replace(re, "/")).getTime();
		let nowTime = new Date().getTime();
		return Math.floor((nowTime - time) / 1000 / 60 / 60 / 24)
	},
	getNextDaysDate(date, day) {
		let re = new RegExp("-", "gi");
		let today = new Date(date.replace(re, "/"));
		today.setDate(today.getDate() + (day - 1));
		let tYear = today.getFullYear();
		let tMonth = today.getMonth() + 1;
		let tDate = today.getDate();
		tMonth = tMonth < 10 ? "0" + tMonth : tMonth;
		tDate = tDate < 10 ? "0" + tDate : tDate;
		return tYear + "-" + tMonth + "-" + tDate;
	},
	getPreviouslyDaysDate(day) {
		let today = new Date();
		let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds); //注意，这行是关键代码
		let tYear = today.getFullYear();
		let tMonth = today.getMonth() + 1;
		let tDate = today.getDate();
		tMonth = tMonth < 10 ? "0" + tMonth : tMonth;
		tDate = tDate < 10 ? "0" + tDate : tDate;
		return tYear + "-" + tMonth + "-" + tDate;
	},
	ageNumber: function(birthday) {
		if(birthday){
			let userDate = new Date(birthday);
			let nowDate = new Date();
			let age = nowDate.getFullYear() - userDate.getFullYear();
			let userNowTime = new Date(nowDate.getFullYear() + "/" + (userDate.getMonth() + 1) + "/" + userDate.getDate()).getTime();
			let nowTime = nowDate.getTime();
			if (userNowTime > nowTime) {
				age -= 1;
			}
			return age < 1 ? 1 : age;
		}
		return 1;
	},
	age: function(birthday) {
		let userDate = new Date(birthday);
		let nowDate = new Date();
		let diffTime = nowDate.getTime() - userDate.getTime();
		if (diffTime < 3600000) {
			return Math.floor(diffTime / 1000 / 60) + "分钟";
		} else if (diffTime > 3600000 && diffTime < 86400000) {
			let h = Math.floor(diffTime / 3600000);
			let m = 0;
			if (diffTime % 3600000 > 0) {
				m = Math.floor((diffTime % 3600000) / 1000 / 60);
			}
			let str = m !== 0 ? m + "分钟" : "";
			return h + "小时" + str;
		} else if (diffTime > 86400000 && diffTime < 2592000000) {
			let d = Math.floor(diffTime / 86400000);
			let h = 0;
			if (diffTime % 86400000 > 0) {
				h = Math.floor((diffTime % 86400000) / 3600000);
			}
			let str = h !== 0 ? h + "小时" : "";
			return d + "天" + str;
		} else if (diffTime > 2592000000 && diffTime < 31104000000) {
			let M = Math.floor(diffTime / 2592000000);
			let d = 0;
			if (diffTime % 2592000000 > 0) {
				d = Math.floor((diffTime % 2592000000) / 86400000);
			}
			let str = d !== 0 ? d + "天" : "";
			return M + "个月零" + str;
		} else {
			let age = nowDate.getFullYear() - userDate.getFullYear();
			let userNowTime = new Date(nowDate.getFullYear() + "/" + (userDate.getMonth() + 1) + "/" + userDate.getDate()).getTime();
			let nowTime = nowDate.getTime();
			if (userNowTime > nowTime) {
				age -= 1;
			}
			return age + "岁";
		}
	},
	// 计算年龄函数
	calculateAge: function(birthDate, specifiedDate) {
		// 确保输入是有效的日期对象
		if (!(birthDate instanceof Date) || !(specifiedDate instanceof Date)) {
			throw new Error('Invalid date objects');
		}
		
		// 如果指定日期早于出生日期，返回0岁0个月
		if (specifiedDate < birthDate) {
			return {
				ageYear: 0,
				ageMonth: 0
			};
		}
		
		const birthYear = birthDate.getFullYear();
		const birthMonth = birthDate.getMonth();
		const birthDay = birthDate.getDate();
		
		const specYear = specifiedDate.getFullYear();
		const specMonth = specifiedDate.getMonth();
		const specDay = specifiedDate.getDate();
		
		// 计算基础年份差和月份差
		let years = specYear - birthYear;
		let months = specMonth - birthMonth;
		
		// 处理天数不足整月的情况
		if (specDay < birthDay) {
			months--;
			// 处理跨年借位
			if (months < 0) {
				years--;
				months += 12;
			}
		}
		
		// 处理月份负数（未跨年但月份不足）
		if (months < 0) {
			years--;
			months += 12;
		}
		
		return {
			ageYear: years,
			ageMonth: months
		};
	}

});